package com.so206.crond;

import com.so206.plugins.MyCacheManager;
import com.so206.po.SystemCcConfig;
import com.so206.po.SystemCcRecord;
import com.so206.service.SystemCcService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Set;

/**
 * cc 记录线程
 */
@Component
public class SystemCcRecordTask {

    private static final Logger logger = LoggerFactory.getLogger(SystemCcRecordTask.class);

    @Autowired
    private SystemCcService ccService;

    @Autowired
    private MyCacheManager myCacheManager;

    public SystemCcRecordTask() {
        logger.info("SystemCcRecordTask Init...");
    }

    /**
     * 记录防御信息 - 5秒一次
     */
    @Async
    @Scheduled(cron = "*/5 * * * * ?")
    public void timerToNow() {
        SystemCcConfig config = ccService.getconfig(1L);
        if (config == null) {
            config = ccService.getconfig(1L);
            myCacheManager.set("config", config, 60000);
        }
        if (config.getRecordStatus() == 1) {
            Set<String> set = MyCacheManager.cache.keySet();
            for (String s : set) {
                if (!s.equals("config") && !s.contains("method:") && !s.contains("url:")) {
                    Integer count = myCacheManager.get(s);
                    if (count == null) {
                        continue;
                    }
                    if (count < config.getReqCount()) {
                        continue;
                    }
                    String method = myCacheManager.get("method:" + s);
                    if (method == null || method.trim().length() == 0) {
                        continue;
                    }
                    String code2 = myCacheManager.get("url:" + s);
                    if (code2 == null || code2.trim().length() == 0) {
                        continue;
                    }
                    SystemCcRecord record = ccService.find_record_by_ip(s);
                    if (record != null) {
                        record.setMethod(method);
                        record.setReqCount(record.getReqCount() + count);
                        record.setUrl(code2);
                        ccService.update_record(record);
                    } else {
                        SystemCcRecord model = new SystemCcRecord();
                        model.setReqCount(count);
                        model.setUrl(code2);
                        model.setIp(s);
                        model.setCreateTime(new Date());
                        model.setOptTime(new Date());
                        ccService.save_record(model);
                    }
                    myCacheManager.set(s, 0, 600000);
                    logger.info("检测到CC攻击,IP" + s + " URL:" + code2 + " QPS:" + count);
                    System.gc();
                }
            }
        }
    }
}
